
from WtfEngine import *;
from Control import *;

class Game(GameObject):
	def __init__(self):
		opts = GameObjectCreationOptions();
		opts.name = "TbsGame";
		# CustomGameObject.__init__(self, opts);
		
		self.societies = [];
		self.turn = 0;
		self.year = -4000;
		self.turnEndCallbacks = [];
		self.turnStartCallbacks = [];
		self.newsItems = [];
		self.newsHistory = {};
		self.players = [];
		
	def StartGame(self, opts):
		assert (self.turn == 0);
		self.players = opts.players;
		self.turn = 1;
		self.newsItems.append(NewsItem("Empire",
			"Your glorious Empire begins..", lambda: 0));
		
		self.StartTurn();
	
	def EndTurn(self):
		map(lambda f: f(), self.turnEndCallbacks);
		
		# Wait for other players (turn blocking)
		Controller.GetInst().OnGameSync(lambda: self.EndTurnAction());
	
	def StartTurn(self):
		map(lambda f: f(), self.turnStartCallbacks);
		
		self.newsHistory[self.turn - 1] = self.newsItems;
		self.newsItems = [];
	
	def EndTurnAction(self):
		"""Performs actions which modify the game state - to be performed \
by game host when all players are synchronized on end turn."""
		
		# Do Trading
		map(lambda p: p.society.DoTrading(), self.players);
		
		self.turn += 1;
		
		# Wait for other players (to finish processing)
		Controller.GetInst().OnGameSync(lambda: self.StartTurn());
		
	def GetDateString(self):
		if(self.year < 0):
			return str(-self.year) + " BC";
		else:
			return str(self.year) + " AD";

class NewsItem:
	def __init__(self, category, text, infoCallback):
		self.category = category;
		self.text = text;
		self.infoCallback = infoCallback;
		